package dk.lector.datasource;


import dk.lector.R;
import dk.lector.domain.TSSystemIntent;
import dk.lector.exception.TSMobileException;
import dk.lector.providers.CONFIGContentProvider;
import dk.lector.utils.ConfigXMLParser;
import android.content.ContentProviderClient;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.util.Log;

public class ConfigDBRepository {
	public static final String TAG = ConfigDBRepository.class.getSimpleName();
	
	public static void saveConfig(Context context){
		// Henter data ud fra ConfigXML og returnerer det som ContentValues
		// Insert metode p� ConfigProvider, sletter gammel Config Table, laver
		// nyt og inds�tter data.
		ContentValues values = ConfigXMLParser.getValuesFromXML(context);
		ContentProviderClient client = context.getContentResolver().acquireContentProviderClient(context.getString(R.string.config_authority));
		
		try {
			int deleted = client.delete(CONFIGContentProvider.CONTENT_URI, null, null);
			Log.d(TAG, "Rows deleted before insert: " + deleted);
			Uri uri = client.insert(CONFIGContentProvider.CONTENT_URI, values);
					
			if(!uri.equals(Uri.EMPTY)){
			context.sendBroadcast(new TSSystemIntent(R.string.ACTIONCODE_CONFIG_DOWNLOADED, TAG));
			}
			
			Log.d(TAG, "URI retured from insert: " + uri);
		} catch (RemoteException e) {
			throw new TSMobileException(e, ConfigDBRepository.class, "saveConfig", "Error during insert");
		}
		finally{client.release();}
	}

	public static String getValue(Context context, String tag) {
		String s = "";
		Cursor c = null;
		ContentProviderClient client = null;
	
			try {
				client = context.getContentResolver().acquireContentProviderClient(context.getString(R.string.config_authority));
				c = client.query(CONFIGContentProvider.CONTENT_URI,
						new String[] { tag }, null, null, null);
				if (c.moveToFirst())
					s = c.getString(c.getColumnIndex(tag));
				return s;
			} catch (NullPointerException e) {
				throw new TSMobileException(e, ConfigDBRepository.class, "getValue", "No such tag!");
			}catch(RemoteException e1)
			{
				throw new TSMobileException(e1, ConfigDBRepository.class, "getValue", "Error during read");
			}
			finally
			{
				if(c!=null) c.close();
				client.release();	
			}
	}

	public static boolean hasConfig(Context context) {
		boolean hasConfig = false;
		Cursor c = null;
		ContentProviderClient client = null;
		
		try {
			client = context.getContentResolver().acquireContentProviderClient(context.getString(R.string.config_authority));
			c = client.query(CONFIGContentProvider.CONTENT_URI,null, null, null, null);
			int count = c.getCount();
			c.close();
			Log.d("Config Repository", "Count: " + count);
			if (count > 0)
				hasConfig = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			if(c!=null) c.close();
			client.release();	
		}
		return hasConfig;
	}
	public static Cursor readConfigCursor(Context context)
	{
		ContentProviderClient client = context.getContentResolver().acquireContentProviderClient(context.getString(R.string.config_authority));
		Cursor c = null;
		try {
			c = client.query(CONFIGContentProvider.CONTENT_URI, CONFIGContentProvider.PROJECTION, null, null, null);
		} catch (RemoteException e) {
			throw new TSMobileException(e, UserDBRepository.class, "readUserCursor", "Error during create");
		}
		finally{client.release();}
		
		return c;
	}
	
	
}
